package sword.offer;

/**
 * 给定单向链表的头指针和一个要删除的节点的值，定义一个函数删除该节点。
 * 返回删除后的链表的头节点
 *
 * @author 胡宇轩
 * @link https://leetcode-cn.com/problems/shan-chu-lian-biao-de-jie-dian-lcof/
 * @Email: yuxuan.hu01@bianlifeng.com
 */
public class Eighteen {
    class Solution {
        public ListNode deleteNode(ListNode head, int val) {
            if (head == null) {
                return null;
            }
            if(head.val == val){
                return head.next;
            }
            ListNode one = head;
            ListNode two = head.next;

            while (two != null) {
                if (two.val == val) {
                    one.next = two.next;
                }
                one = one.next;
                two = two.next;
            }
            return head;
        }
    }

    class ListNode {
        int val;
        ListNode next;

        ListNode(int x) {
            val = x;
        }
    }


}
